/*
Copyright 2005-2014 CP Fricano - All Rights Reserved

This software and source code is licensed under the Apache
License, Version 2.0. You may not use this software or source
code except in compliance with the License.  You may obtain a
copy of the License at: http://www.apache.org/licenses/LICENSE-2.0.
Use and distributed under this License is on an "as is" basis,
without expressed or implied warranties or conditions of any kind.
See the License for the specific language governing permissions and
limitations.
 */

package com.fricano.java42.instructional.example.swing;

import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;

public class J42JLabel_01 extends JLabel {

    private static final long serialVersionUID = -42L;

    private JWindow           jWindow          = null;

    private Timer             timer            = null;

    public J42JLabel_01(final String string) {
        super(string);
        setOpaque(true);
    }

    public JWindow moveIt(final JFrame jFrame) {
        System.out.println(SwingUtilities.isEventDispatchThread());
        if (jWindow == null) {
            jFrame.getContentPane().remove(this);
            Toolkit.getDefaultToolkit().beep();
            jWindow = new JWindow(jFrame) {
                private static final long serialVersionUID = -42L;

                @Override
                public void dispose() {
                    Toolkit.getDefaultToolkit().beep();
                    timer.cancel();
                    super.dispose();
                }
            };
            jWindow.setAlwaysOnTop(true);
            jWindow.getContentPane().setLayout(new FlowLayout());
            jWindow.getContentPane().add(this);
            jWindow.pack();
            jWindow.setVisible(true);
            timer = new Timer(true);
            timer.scheduleAtFixedRate(new TimerTask() {
                @Override
                public void run() {
                    final SwingWorker<String, String> x = new SwingWorker<String, String>() {
                        @Override
                        protected String doInBackground() throws Exception {
                            System.out.println(" xx " + SwingUtilities.isEventDispatchThread());
                            return "";
                        }

                        @Override
                        protected void done() {
                            System.out.println(" done " + SwingUtilities.isEventDispatchThread());
                        }
                    };
                    x.execute();
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            jWindow.setLocation(jWindow.getLocation().x + 25, jWindow.getLocation().y + 24);
                            if (jWindow.getLocation().x > Toolkit.getDefaultToolkit().getScreenSize().width / 2) {
                                jWindow.setLocation(0, 0);
                            }
                            if (jWindow.getLocation().y > Toolkit.getDefaultToolkit().getScreenSize().height / 2) {
                                jWindow.setLocation(0, 0);
                            }
                        }
                    });
                }
            }, 0, 500);
        }
        else {
            jFrame.getContentPane().add(this);
            jWindow.dispose();
            jWindow = null;
        }
        return jWindow;
    }

}
